<template>
	<view class="page">
		<z-paging ref="paging" v-model="searchList" @query="queryList" :loading-more-enabled="false"
			empty-view-img="/reading/static/noBook.gif"
			:empty-view-img-style="{'border-radius': '50%','width':'150px','height':'150px'}"
			empty-view-text="搜索吧"
			:empty-view-title-style="{'font-size':'15px','margin-top':'30px'}">
			<template #top >
				<view style="text-align: center; padding-bottom: 10px;" slot="top">
								<view style="display: flex; justify-content: space-between; align-items: center;">
				<!-- 当前无数据,检查网络是否正常.\n检查是否配置数据源:我的-数据源配置 -->
									<uni-search-bar class="search" style="width: 100%;" @confirm="search()" v-model="searchKey"
										placeholder="输入关键字查询" @cancel="isSearch=false">
									</uni-search-bar>
								</view>
								<view style="display: flex; padding-left: 20px; padding-right: 20px; flex-wrap: wrap;">
									<uni-tag v-for="item in history" type="default" size="small" inverted :text="item"
										style="margin-top: 10px; margin-right: 8px; margin-left: 8px;" @click="tagAction(item)">
									</uni-tag>
								</view>
							</view>
			</template>
			


			<view class="content">
				<uni-list style="width: 100%;height: 100%;">
					<uni-list-item clickable v-for="(item,index) in searchList" :key="index" :title="item.title"
						:note="item.autor" :rightText="item.time" @click="selectItem(index)" />
				</uni-list>
			</view>
		</z-paging>
		<moveView tabTitle='搜索'></moveView>
	</view>
</template>
<script>
	
	// import ijs from "./index.js";
	// export default ijs;
	import moveView from '/reading/components/moveView.vue'
	export default {
		components:{
			moveView
		},
		data() {
			return {
				searchKey: '',
				searchList: [],
				list: [],
				isSearch: false,
	
				history: uni.getStorageSync("searchHistory"),
	
			}
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: "搜索"
			});
	
	
	
		},
		onShow() {
	
		},
		methods: {
			tagAction(title) {
				this.searchKey = title;
				this.$refs.paging.reload();
			},
			queryList() {
				this.search();
			},
			search() {
	
				if (!this.searchKey || this.searchKey.length == 0) {
					this.$refs.paging.complete([]);
					return;
				}
				this.isSearch = true;
				uni.showLoading({
					title: '搜索中'
				});
				if (this.history.indexOf(this.searchKey) == -1) {
					if (!this.history || this.history.length == 0) {
						this.history = [];
					}
					this.history.unshift(this.searchKey);
					if (this.history.length > 15) {
						this.history.pop()
					}
					uni.setStorage({
						key: "searchHistory",
						data: this.history
					})
				}
	
	
	
				uniCloud.callFunction({
					name: 'router',
					data: {
						action: 'book/searchBqg',
						data: {
							searchKey: this.searchKey,
						}
					}
				}).then(res => {
					uni.hideLoading();
					// this.searchList = res.result;
					this.$refs.paging.complete(res.result);
				}).catch((err) => {
					uni.hideLoading();
				})
	
			},
	
			selectItem(index) {
				var model = this.searchList[index];
				uni.navigateTo({
	
					url: `/reading/pages/read/index?title=${model.title}&url=${encodeURIComponent(model.link)}`
				});
			}
		}
	}
</script>
<style lang="scss">
	@import url("./index.scss");
</style>